<script setup lang="ts">
import { ref } from 'vue'
import type { TableInstance } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

interface ConsultationRecord {
  id: number
  studentName: string
  counselorName: string
  consultationTime: string
  type: string
  duration: string
  summary: string
  followUpPlan: string
  status: string
}

const tableData = ref<ConsultationRecord[]>([
  {
    id: 1,
    studentName: '张三',
    counselorName: '张医生',
    consultationTime: '2024-01-15 14:00',
    type: '个人咨询',
    duration: '50分钟',
    summary: '学生反映学业压力大，存在睡眠问题',
    followUpPlan: '建议每周一次跟进咨询，关注睡眠质量改善情况',
    status: '已完成'
  },
  {
    id: 2,
    studentName: '李四',
    counselorName: '李医生',
    consultationTime: '2024-01-16 10:00',
    type: '个人咨询',
    duration: '45分钟',
    summary: '存在人际交往障碍，社交焦虑明显',
    followUpPlan: '制定社交技能训练计划，两周后复诊',
    status: '已完成'
  }
])

const searchQuery = ref('')
const currentPage = ref(1)
const pageSize = ref(10)

const handleSearch = () => {
  // TODO: 实现搜索功能
}

const handleEdit = (row: ConsultationRecord) => {
  // TODO: 实现编辑功能
}

const handleDelete = (row: ConsultationRecord) => {
  // TODO: 实现删除功能
}

const detailVisible = ref(false)
const currentRecord = ref<ConsultationRecord>()

const handleViewDetail = (row: ConsultationRecord) => {
  currentRecord.value = row
  detailVisible.value = true
}
</script>

<template>
  <el-dialog v-model="detailVisible"
    title="咨询记录详情"
    width="50%"
    :before-close="() => detailVisible = false">
    <el-descriptions :column="1"
      border>
      <el-descriptions-item label="学生姓名">
        {{ currentRecord?.studentName }}
      </el-descriptions-item>
      <el-descriptions-item label="咨询师">
        {{ currentRecord?.counselorName }}
      </el-descriptions-item>
      <el-descriptions-item label="咨询时间">
        {{ currentRecord?.consultationTime }}
      </el-descriptions-item>
      <el-descriptions-item label="咨询类型">
        {{ currentRecord?.type }}
      </el-descriptions-item>
      <el-descriptions-item label="咨询时长">
        {{ currentRecord?.duration }}
      </el-descriptions-item>
      <el-descriptions-item label="咨询摘要">
        <div style="white-space: pre-line">{{ currentRecord?.summary }}</div>
      </el-descriptions-item>
      <el-descriptions-item label="跟进计划">
        <div style="white-space: pre-line">{{ currentRecord?.followUpPlan }}</div>
      </el-descriptions-item>
      <el-descriptions-item label="状态">
        <el-tag type="success"
          effect="dark">
          {{ currentRecord?.status }}
        </el-tag>
      </el-descriptions-item>
    </el-descriptions>
  </el-dialog>

  <div class="consultation-records-container">
    <div class="header">
      <h2>咨询记录管理</h2>
      <div class="actions">
        <el-input v-model="searchQuery"
          placeholder="搜索咨询记录"
          class="search-input"
          @keyup.enter="handleSearch">
          <template #append>
            <el-button @click="handleSearch">
              <el-icon>
                <Search />
              </el-icon>
            </el-button>
          </template>
        </el-input>
        <el-button type="primary">新增记录</el-button>
      </div>
    </div>

    <el-table :data="tableData"
      style="width: 100%"
      table-layout="fixed"
      border>
      <el-table-column prop="id"
        label="ID"
        width="80"
        align="center" />
      <el-table-column prop="studentName"
        label="学生姓名"
        width="120" />
      <el-table-column prop="counselorName"
        label="咨询师"
        width="120" />
      <el-table-column prop="consultationTime"
        label="咨询时间"
        width="180" />
      <el-table-column prop="type"
        label="咨询类型"
        width="120" />
      <el-table-column prop="duration"
        label="时长"
        width="100"
        align="center" />
      <el-table-column prop="summary"
        label="咨询摘要"
        min-width="200"
        show-overflow-tooltip />
      <el-table-column prop="followUpPlan"
        label="跟进计划"
        min-width="200"
        show-overflow-tooltip />
      <el-table-column prop="status"
        label="状态"
        width="100"
        align="center">
        <template #default="{ row }">
          <el-tag type="success"
            effect="dark">{{ row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作"
        width="200"
        align="center"
        fixed="right">
        <template #default="{ row }">
          <el-button link
            type="primary"
            @click="handleViewDetail(row)">查看</el-button>
          <el-button link
            type="primary"
            @click="handleEdit(row)">编辑</el-button>
          <el-button link
            type="danger"
            @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="50"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper" />
    </div>
  </div>
</template>

<style scoped>
.consultation-records-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.actions {
  display: flex;
  gap: 10px;
}

.search-input {
  width: 300px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
